<template>
    <div id="box">
    <!--这里必须使用一个双标签-->
    <div id="login_box">
        <div class="img_position">
            <el-avatar :size="140" :src="imgUrl"></el-avatar>
        </div>
        <el-card class="box-card">
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="用户名" prop="name">
                    <el-input type="text" v-model="ruleForm.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary"
                               size="mini"
                               :plain="true"
                               @click="login"
                               style="margin-left: 100px;width: 100px">登录</el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
    </div>
</template>

<script>
    export default {
        name: "Login",
        data(){
             return {
                 ruleForm: {
                     name: '',
                     password: ''
                 },
                 rules: {
                     name: [
                         {required: true, message:'用户名不能为空', trigger: 'blur'},
                     ],
                     password: [
                         {required: true, message: '密码不能为空', trigger: 'blur'},
                     ]
                 },
                 imgUrl:'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png'
             }
        },

        methods:{
            login(){
                //表单校验
                this.$refs['ruleForm'].validate((valid) => {
                        if(valid){
                             //url:后端登录接口的路径
                             this.$http.post("http://localhost:8808/system/login",this.ruleForm).then(result=>{
                                  if(result.data.code===2000){
                                      //获取登录成功后的token
                                      var token = result.data.data;
                                      //把token保存 sessionStorage理解为session
                                      sessionStorage.setItem("token",token)
                                      //路由跳转
                                      this.$router.push("/home")
                                      // //外网跳转
                                      // window.location.href="";
                                  }
                             });
                        }
                })
            }
        }
    }
</script>

<style>

    #box{
         height: 100%;
         background: url("../assets/bg.jpg");
    }
    #login_box{
        position: relative;
        width: 500px;
        margin: 250px auto;
    }
    #login_box div.img_position{
        position: absolute;
        left: 200px;
        top: -70px;
    }
    .text {
        font-size: 14px;
    }

    .item {
        padding: 18px 0;
    }

    .box-card {
        padding: 100px 50px 0 0;
        width: 480px;
    }

</style>